import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/purchase_supplier_level_odoo_api.dart';
import 'package:flutter_admin/models/odoo/purchase_supplier_level.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import '../../../generated/l10n.dart';

class PurchaseSupplierLevelEdit extends StatefulWidget {
  PurchaseSupplierLevelEdit({this.purchaseSupplierLevel});

  final PurchaseSupplierLevel? purchaseSupplierLevel;

  @override
  _PurchaseSupplierLevelEditState createState() =>
      _PurchaseSupplierLevelEditState();
}

class _PurchaseSupplierLevelEditState extends State<PurchaseSupplierLevelEdit> {
  late PurchaseSupplierLevel purchaseSupplierLevel;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  List<SelectOptionVO> templateList = [];

  @override
  void initState() {
    super.initState();
    _loadSelectVO();
    this.isAdd = widget.purchaseSupplierLevel == null;
    this.purchaseSupplierLevel =
        widget.purchaseSupplierLevel ?? PurchaseSupplierLevel();
    setState(() {});
  }

  _loadSelectVO() async {
    templateList = await DynamicDataApi.getModelSelect(
      model: "purchase.evaluation.template",
      name: "template_name",
      active: false,
    );
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelect(
            width: 300,
            label: '*模板名称',
            dataList: templateList,
            value: purchaseSupplierLevel.templateId,
            onSaved: (v) {
              purchaseSupplierLevel.templateId = v;
            },
          ),
          CryInput(
            width: 300,
            label: '等级名称',
            value: purchaseSupplierLevel.levelName,
            onSaved: (v) {
              purchaseSupplierLevel.levelName = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInputNum(
            width: 300,
            label: '上限',
            value: purchaseSupplierLevel.upper,
            onSaved: (v) {
              purchaseSupplierLevel.upper = v;
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : int.parse(v) >= 0 && int.parse(v) <= 100
                      ? null
                      : "必须在0到100之间";
            },
          ),
          CryInput(
            width: 300,
            label: '等级规则',
            value: purchaseSupplierLevel.levelRule,
            onSaved: (v) {
              purchaseSupplierLevel.levelRule = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInputNum(
            width: 300,
            label: '下限',
            value: purchaseSupplierLevel.lower,
            onSaved: (v) {
              purchaseSupplierLevel.lower = v;
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : int.parse(v) >= 0 && int.parse(v) <= 100
                      ? null
                      : "必须在0到100之间";
            },
          ),
          CryInput(
            width: 300,
            label: '等级说明',
            value: purchaseSupplierLevel.comment,
            onSaved: (v) {
              purchaseSupplierLevel.comment = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            PurchaseSupplierLevelOdooApi.saveOrUpdate(
                    purchaseSupplierLevel.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
            // });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
